package DFS;

import java.util.ArrayList;
import java.util.List;

public class Solution46 {


    public static void main(String[] args) {


        int[] nums = {1, 2, 3};
        System.out.println(permute(nums));
    }

    public static List<List<Integer>> permute(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();
        List<Integer> chain = new ArrayList<>();
        boolean[] pb = new boolean[nums.length];
        dfs(nums, 0, pb, chain, res);
        return res;
    }

    public static void dfs(int[] nums, int level, boolean[] pb, List<Integer> chain, List<List<Integer>> res) {
        //截止条件
        if (chain.size() == nums.length) {
            res.add(new ArrayList<>(chain));
            return;
        }

        //遍历筛选节点
        for (int i = 0; i < nums.length; i++) {
            if (!pb[i]) {
                chain.add(nums[i]);
                pb[i] = true;
                dfs(nums, i + 1, pb, chain, res);
                pb[i] = false;
                chain.remove(chain.size() - 1);
            }
        }
    }

}
